package com.zs.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.system.comm.util.DateUtils;
import com.system.comm.util.JedisUtil;
import com.system.common.pojo.BisBeautyConfig;
import com.system.common.pojo.SysUser;
import com.zs.service.BisBeautyConfigService;

import redis.clients.jedis.Jedis;
@Controller
@RequestMapping("Config")
public class BisBeautyConfigController {
	 
	@Autowired
	private BisBeautyConfigService beautyConfigService;

	
	@RequestMapping("queryConfig")
	public String query(Model model,HttpServletRequest request){
		List<BisBeautyConfig> configList=this.beautyConfigService.queryBisBeautyConfig();
		model.addAttribute("configList", configList);
		String money=request.getParameter("money");
		String validDate=request.getParameter("validDate");
	    Jedis jedis=JedisUtil.getJedisClient();
	    if (money!=null&&!money.equals("")&&validDate!=null&&!validDate.equals("")) {
	    	JedisUtil.setString("money", money, 0);
		    JedisUtil.setString("validDate",validDate, 0);
		}
	    model.addAttribute("money", JedisUtil.getString("money", ""));
	    model.addAttribute("validDate", JedisUtil.getString("validDate", ""));
	    JedisUtil.close(jedis);
		return "BisShopUser/ShowBisBeautyConfig";		 
	}
	
	@RequestMapping("DelConfig")
	public String delete(String configId){
		String[] id=configId.split(",");
		for (String strid : id) {
			this.beautyConfigService.delConfig(strid);
		}
		return "redirect:/Config/queryConfig";
	}
	
	@RequestMapping("SavaConfig")
	public String sava(BisBeautyConfig beautyConfig,HttpSession session){
		beautyConfig.setConfigId(DateUtils.getDateStr(new Date()));
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			beautyConfig.setCreateTime(dateFormat.parse(dateFormat.format(new Date())));
			beautyConfig.setModifyTime(dateFormat.parse(dateFormat.format(new Date())));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		beautyConfig.setIsDel("0");
		SysUser sysUser=(SysUser)session.getAttribute("user");
	       if (sysUser!=null) {
	           beautyConfig.setCreateUserId(sysUser.getUserId());
	           beautyConfig.setModifyUserId(sysUser.getUserId());
		   }
		this.beautyConfigService.savaConfig(beautyConfig);
		return "redirect:/Config/queryConfig";
	}
	
	@RequestMapping("EditConfig")
	public String edit(BisBeautyConfig beautyConfig){
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			beautyConfig.setModifyTime(dateFormat.parse(dateFormat.format(new Date())));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.beautyConfigService.editConfig(beautyConfig);
		return "redirect:/Config/queryConfig";
	}
}
